package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.domain.HealthAssessment;
import com.zzyl.nursing.dto.CancelNursingTaskDto;
import com.zzyl.nursing.dto.NursingTaskDto;

import com.zzyl.nursing.service.IElderService;
import com.zzyl.nursing.service.IHealthAssessmentService;
import com.zzyl.nursing.util.IDCardUtils;
import com.zzyl.nursing.vo.NursingTaskVo;
import com.zzyl.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingTaskMapper;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.service.INursingTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
/**
 * 护理任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@Service
public class NursingTaskServiceImpl extends ServiceImpl<NursingTaskMapper,NursingTask> implements INursingTaskService
{
    @Autowired
    private NursingTaskMapper nursingTaskMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IElderService elderService;
    @Autowired
    private IHealthAssessmentService healthAssessmentService;

    /**
     * 查询护理任务
     * 
     * @param id 护理任务主键
     * @return 护理任务
     */
    @Override
    public NursingTaskVo selectNursingTaskById(Long id)
    {
        NursingTask nursingTask = getById(id);
        NursingTaskVo vo = BeanUtil.toBean(nursingTask, NursingTaskVo.class);
        Long elderId = nursingTask.getElderId();
        String[] split = nursingTask.getNursingId().split(",");
        List<String> nursingName=new ArrayList<>();
        for (String s : split) {
            Long nursingId = Long.valueOf(s);
            SysUser sysUser = sysUserService.selectUserById(nursingId);
            nursingName.add(sysUser.getNickName());
        }
        vo.setNursingName(nursingName);
        Elder elder = elderService.selectElderById(elderId);
        vo.setElderName(elder.getName());
        vo.setSex(elder.getSex()==1?"男":"女");
        vo.setAge(IDCardUtils.getAgeByIdCard(elder.getIdCardNo()));
        HealthAssessment healthAssessment = healthAssessmentService.lambdaQuery()
                .eq(HealthAssessment::getIdCard, elder.getIdCardNo())
                .one();
        if (ObjectUtil.isNotEmpty(healthAssessment)) {
            vo.setNursingLevelName(healthAssessment.getNursingLevelName());
        }
        if(ObjectUtil.isNotEmpty(vo.getUpdateBy())){
            Long updateByID = Long.valueOf(vo.getUpdateBy());
            SysUser sysUser = this.sysUserService.selectUserById(updateByID);
            String nickName = sysUser.getNickName();
            vo.setUpdater(nickName);
        }
        return vo;
    }

    /**
     * 查询护理任务列表
     * 
     * @param nursingTask 护理任务
     * @return 护理任务
     */
    @Override
    public List<NursingTask> selectNursingTaskList(NursingTask nursingTask)
    {
        return nursingTaskMapper.selectNursingTaskList(nursingTask);
    }

    @Override
    public TableDataInfo<NursingTaskVo> selectNursingTaskList(NursingTaskDto dto) {
        Page<NursingTask> page = Page.of(dto.getPageNum(),dto.getPageSize());
        this.lambdaQuery()
                .like(ObjectUtil.isNotEmpty(dto.getElderName()),NursingTask::getElderName,dto.getElderName())
                .like(ObjectUtil.isNotEmpty(dto.getNurseId()),NursingTask::getNursingId,dto.getNurseId())
                .eq(ObjectUtil.isNotEmpty(dto.getProjectId()),NursingTask::getProjectId,dto.getProjectId())
                .eq(ObjectUtil.isNotEmpty(dto.getStatus()),NursingTask::getStatus,dto.getStatus())
                .between(ObjectUtil.isNotEmpty(dto.getStartTime())&&ObjectUtil.isNotEmpty(dto.getEndTime()),NursingTask::getEstimatedServerTime,dto.getStartTime(),dto.getEndTime())
                .page(page);
        List<NursingTaskVo> list = BeanUtil.copyToList(page.getRecords(), NursingTaskVo.class);
        //List<String> nursingName
        list.forEach(item->{
            String[] split = item.getNursingId().split(",");
            List<String> nursingName=new ArrayList<>();
            for (String s : split) {
                Long id = Long.valueOf(s);
                SysUser sysUser = sysUserService.selectUserById(id);
                nursingName.add(sysUser.getNickName());
            }
            item.setNursingName(nursingName);
        });
        TableDataInfo tableDataInfo = new TableDataInfo(list, page.getTotal());
        return tableDataInfo;
    }

    /**
     * 新增护理任务
     * 
     * @param nursingTask 护理任务
     * @return 结果
     */
    @Override
    public int insertNursingTask(NursingTask nursingTask)
    {
        return save(nursingTask)?1:0;
    }

    /**
     * 修改护理任务
     * 
     * @param nursingTask 护理任务
     * @return 结果
     */
    @Override
    public int updateNursingTask(NursingTask nursingTask)
    {
        return updateById(nursingTask)?1:0;
    }

    /**
     * 批量删除护理任务
     * 
     * @param ids 需要删除的护理任务主键
     * @return 结果
     */
    @Override
    public int deleteNursingTaskByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除护理任务信息
     * 
     * @param id 护理任务主键
     * @return 结果
     */
    @Override
    public int deleteNursingTaskById(Long id)
    {
        return removeById(id)?1:0;
    }

    /**
     * 取消任务
     *
     * @param dto
     */
    @Override
    public void cancelTask(CancelNursingTaskDto dto) {
        this.lambdaUpdate()
                .set(NursingTask::getCancelReason,dto.getReason())
                .set(NursingTask::getStatus,3)
                .set(NursingTask::getUpdateTime, LocalDateTime.now())
                .set(NursingTask::getUpdateBy, String.valueOf(SecurityUtils.getUserId()))
                .eq(NursingTask::getId,dto.getTaskId())
                .update();
    }

    /**
     * 执行任务
     *
     * @param dto
     */
    @Override
    public void doTask(CancelNursingTaskDto dto) {
        this.lambdaUpdate()
                .set(NursingTask::getTaskImage, dto.getTaskImage())
                .set(NursingTask::getStatus, 2)
                .set(NursingTask::getMark, dto.getMark())
                .set(NursingTask::getRealServerTime, dto.getEstimatedServerTime())
                .set(NursingTask::getUpdateTime, LocalDateTime.now())
                .set(NursingTask::getUpdateBy, String.valueOf(SecurityUtils.getUserId()))
                .eq(NursingTask::getId, dto.getTaskId())
                .update();
    }

    /**
     * 执行任务
     *
     * @param dto
     */
    @Override
    public void updateTime(CancelNursingTaskDto dto) {
        this.lambdaUpdate()
                .set(NursingTask::getEstimatedServerTime, dto.getEstimatedServerTime())
                .set(NursingTask::getUpdateTime, LocalDateTime.now())
                .set(NursingTask::getUpdateBy, String.valueOf(SecurityUtils.getUserId()))
                .eq(NursingTask::getId, dto.getTaskId())
                .update();
    }
}
